﻿using System;
using System.Xml;

namespace Quail.Silverlight
{
    /// <summary>
    /// Wraps the silverlight combo box
    /// </summary>
    public class SlvComboBox : SlvControl
    {
        private const char ValueDelimeter = '|';
        private XmlDocument _xmlItems;
    	private string _values;
        private SlvComboBoxItems _items;

        /// <summary>
        /// Initializes a new instance of the <see cref="SlvComboBox"/> class.
        /// </summary>
        public SlvComboBox()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SlvComboBox"/> class.
        /// </summary>
        /// <param name="container">The container for the control.</param>
        /// <param name="selector">The selector.</param>
        public SlvComboBox(SlvControlContainer container, string selector)
            : base(container, selector)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SlvComboBox"/> class.
        /// </summary>
        /// <param name="control">
        /// The control.
        /// </param>
        public SlvComboBox(SlvControl control)
            : base(control)
        {
        }


        /// <summary>
        /// Gets the item count.
        /// </summary>
        public int ItemsCount
        {
            get
            {
                return int.Parse(GetPropertyValue("ItemsCount"));
            }
        }

        /// <summary>
        /// Gets or sets SelectedIndex.
        /// </summary>
        public int SelectedIndex
        {
            get { return int.Parse(GetPropertyValue("SelectedIndex")); }
            set { SetPropertyValue("SelectedIndex", value.ToString()); }
        }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public string Value
        {
            get { return GetPropertyValue("Value"); }
            set { SetPropertyValue("Value", value); }
        }

        /// <summary>
        /// Gets Values.
        /// </summary>
        public string[] Values
    	{
    		get
    		{
				return GetPropertyValue("ValuesAsXml").Split(ValueDelimeter);
    		}
    	}

        /// <summary>
        /// Gets ValuesAsXml.
        /// </summary>
        public XmlDocument ValuesAsXml
		{
			get
			{
				_xmlItems = new XmlDocument();

				foreach (var value in Values)
				{
					XmlDocument doc = new XmlDocument();
					doc.LoadXml(value);

					XmlElement root = doc.DocumentElement;
					if (root != null)
					{
						_xmlItems.AppendChild(root);
					}
				}

				return _xmlItems;
			}
		}

        /// <summary>
        /// Gets the list of items
        /// </summary>
        public SlvComboBoxItems Items
        {
            get
            {
                LoadItems();
                return _items;
            }
        }

        /// <summary>
        /// Selects a random item from the combobox (box must have items in it!)
        /// </summary>
        /// <returns>the item selected</returns>
        public int SelectRandomItem()
        {
            if (Items.Count == 0)
                throw new SlvControlException(this, LogStatus.Fail, "Unable to select a random item: combobox has no items"); 
            var r = new Random();
            var i = r.Next(1, Items.Count + 1);

            SelectedIndex = i;

            return SelectedIndex;
        }

        /// <summary>
        /// Refreshes the list of items
        /// </summary>
        public void RefreshItems()
        {
            _items = null;
            LoadItems();
        }

        /// <summary>
        /// Loads the Items property
        /// </summary>
        public void LoadItems()
        {
            if (_items == null)
            {
                _items = new SlvComboBoxItems(this);
            }
        }

        /// <summary>
        /// Items wrapper for combobox
        /// </summary>
		public class SlvComboBoxItems
        {
            private readonly System.Collections.Generic.List<string> _items;

            /// <summary>
            /// Initializes a new instance of the <see cref="SlvComboBoxItems"/> class.
            /// </summary>
            /// <param name="combo">
            /// The combo.
            /// </param>
            public SlvComboBoxItems(SlvComboBox combo)
            {
                _items = new System.Collections.Generic.List<string>();

                var valueStr = combo.GetPropertyValue("Values");
                var valueArr = valueStr.Split(',');

                foreach (string value in valueArr)
                    _items.Add(value);
            }

            /// <summary>
            /// Gets the count of items
            /// </summary>
            public int Count
            {
                get { return _items.Count; }
            }

            /// <summary>
            /// Gets the item by index
            /// </summary>
            /// <param name="index">the item index</param>
            /// <returns>the string value</returns>
            public string this[int index]
            {
                get { return _items[index]; }
            }
        }
    }
}
